﻿using System.ServiceModel;
using System.Windows;
using StaffWarsFm.Discovery;
using StaffWarsFm.Lib.Data;
using StaffWarsFm.Properties;
using StaffWarsFm.Server;

namespace StaffWarsFm
{
    /// <summary>
    /// Interaction logic for LogonWindow.xaml
    /// </summary>
    public partial class LogonWindow : Window
    {
        private ServiceFinder serviceFinder;
        
        public LogonWindow()
        {
            InitializeComponent();

            DataContext = ViewModel = new LogonViewModel();
        }

        public LogonViewModel ViewModel { get; set; }

        private void StartFinder()
        {
            serviceFinder = new ServiceFinder();

            serviceFinder.SpeakerMasterFound += serviceFinder_SpeakerMasterFound;
            serviceFinder.Find();
        }

        private void StopFinder()
        {
            if (serviceFinder == null) return;
            serviceFinder.SpeakerMasterFound -= serviceFinder_SpeakerMasterFound;
            serviceFinder.Stop();
            serviceFinder = null;
        }

        private void Start(bool isServer)
        {
            Start(isServer, null);
        }

        private void Start(bool isServer, SpeakerMasterDetails speakerMasterDetails)
        {
            StopFinder();
            var user = GetUser();

            // start me as a speaker controller
            if (isServer)
            {
                RadioServiceHost.StartRadioService("localhost", user);
                speakerMasterDetails = new SpeakerMasterDetails
                    {
                        Address = "localhost",
                        User = user
                    };
            }

            var mainWindow = new MainWindow();
            mainWindow.ViewModel.IsServer = isServer;
            mainWindow.Start(user, speakerMasterDetails);

            mainWindow.ViewModel.CurrentUser = user;
            mainWindow.ViewModel.SpeakerMaster = speakerMasterDetails.User;

            mainWindow.Show();
            Close();
        }

        private User GetUser()
        {
            return new User
                {
                    DisplayName = displayName.Text,
                    UserName = lastFmName.Text
                };
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            StartFinder();
        }

        private void serviceFinder_SpeakerMasterFound(object sender, SpeakerMasterFoundEventArgs e)
        {
            Dispatcher.Invoke(() => ViewModel.SpeakerMasters.Add(e.SpeakerMasterDetails));
        }

        private void RefreshSpeakerMasters_Click(object sender, RoutedEventArgs e)
        {
            StopFinder();
            StartFinder();
        }

        private void Window_Closed(object sender, System.EventArgs e)
        {
            StopFinder();
            Settings.Default.Save();
        }

        private void ImSpeakerMaster_Click(object sender, RoutedEventArgs e)
        {
            Start(true);
        }

        private void ConnectToSpeakerMaster_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            var speakerMasterDetails = (SpeakerMasterDetails) e.Parameter;

            Start(false, speakerMasterDetails);
        }
    }
}
